package com.longchat.controller.admin.award;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.longchat.domain.award.Questionnaire;
import com.longchat.domain.award.Questionnaire.QuestionnarireType;
import com.longchat.dto.award.VoteOptionsInfo;
import com.longchat.dto.comm.DatatablesPageViewInfo;
import com.longchat.dto.comm.ResultInfo;
import com.longchat.dto.vote.QuestionnaireInfo;
import com.longchat.dto.vote.VoteDataTablesQueryInfo;
import com.longchat.service.inter.award.IQuestionnaireService;

@Controller("voteManage")
@RequestMapping("/admin/activity/vote")
public class VoteController {

	@Autowired
	private IQuestionnaireService questionnaireService;
	@RequestMapping("")
	public String index(){
		return "/admin/vote/question_list";
	}
	@ResponseBody
	@RequestMapping("/list")
	public DatatablesPageViewInfo<Questionnaire> page(VoteDataTablesQueryInfo info){
		return questionnaireService.findPageBy(info);
	}
	@ResponseBody
	@RequestMapping("/addQuestionnaire")
	public ResultInfo addQuestionnaire(@RequestParam(value="file",required=false)MultipartFile file,QuestionnaireInfo info,HttpSession session){
		String path = session.getServletContext().getRealPath("/upload");
		Questionnaire question = new Questionnaire();
		if(file!=null){
		String originalFileName = file.getOriginalFilename();
		String fileName = UUID.randomUUID().toString().replaceAll("-", "")+"."+originalFileName.substring(originalFileName.lastIndexOf(".")+1);
		File uploadfile = new File(path,fileName);
		if(!uploadfile.getParentFile().exists()){
			uploadfile.getParentFile().mkdirs();
		}
		try { 
			file.transferTo(uploadfile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		question.setCoverImg("/upload/"+fileName);
		}
		question.setName(info.getName());
		question.setDescription(info.getDescription());
		if(info.getType()==0){
			question.setType(QuestionnarireType.SIGNLE);
		}else{
			question.setType(QuestionnarireType.MULTI);
		}
		return questionnaireService.addQuestionnaire(question);
	}
	@RequestMapping("/addQuestionDialog")
	public String addQuestionDialog(){
		return "/admin/vote/question_add";
	}
	@ResponseBody
	@RequestMapping("/updateQuestionnaire")
	public ResultInfo updateQuestionnaire(Questionnaire question){
		return questionnaireService.updateQuestionnaire(question);
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ResultInfo delete(Integer id){
		return questionnaireService.deleteQuestionnaire(id);
	}
	@RequestMapping("/maintainOptionDialog")
	public ModelAndView maintainOptionDialog(Integer id){
		ModelAndView modelAndView = new ModelAndView("/admin/vote/option_maintain");
		modelAndView.addObject("id", id);
		modelAndView.addObject("options", questionnaireService.findOptionsBy(id));
		return modelAndView;
	}
	@ResponseBody
	@RequestMapping("/saveOptions")
	public ResultInfo saveOptions(VoteOptionsInfo info){
		Integer questionid=info.getQuestionid();
		List<String> options = info.getOptions();
		return questionnaireService.saveOptions(questionid,options);
	}
}
